package com.ruoyi.source.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.SourceBaseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.SourceMemberDataInfoTempMapper;
import com.ruoyi.db.domain.SourceMemberDataInfoTemp;
import com.ruoyi.source.service.ISourceMemberDataInfoTempService;

/**
 * 线索用户资质信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-28
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class SourceMemberDataInfoTempServiceImpl implements ISourceMemberDataInfoTempService 
{
    @Autowired
    private SourceMemberDataInfoTempMapper sourceMemberDataInfoTempMapper;

    /**
     * 查询线索用户资质信息
     * 
     * @param id 线索用户资质信息主键
     * @return 线索用户资质信息
     */
    @Override
    public SourceMemberDataInfoTemp selectSourceMemberDataInfoTempById(Long id)
    {
        return sourceMemberDataInfoTempMapper.selectSourceMemberDataInfoTempById(id);
    }

    /**
     * 查询线索用户资质信息列表
     * 
     * @param sourceMemberDataInfoTemp 线索用户资质信息
     * @return 线索用户资质信息
     */
    @Override
    public List<SourceMemberDataInfoTemp> selectSourceMemberDataInfoTempList(SourceMemberDataInfoTemp sourceMemberDataInfoTemp)
    {
        return sourceMemberDataInfoTempMapper.selectSourceMemberDataInfoTempList(sourceMemberDataInfoTemp);
    }

    /**
     * 查询线索用户资质信息列表
     *
     * @param sourceMemberDataInfoTemp 线索用户资质信息
     * @return 线索用户资质信息
     */
    @Override
    public List<Map<String, Object>> selectSourceMemberDataInfoTempMapList(SourceMemberDataInfoTemp sourceMemberDataInfoTemp)
    {
        return sourceMemberDataInfoTempMapper.selectSourceMemberDataInfoTempMapList(sourceMemberDataInfoTemp);
    }


    @Override
    public List<Map<String, Object>> getCityList(SourceMemberDataInfoTemp sourceMemberDataInfoTemp) {
        List<Map<String, Object>> cityCount = sourceMemberDataInfoTempMapper.getCityCount(sourceMemberDataInfoTemp);
        Map<String, List<Map<String, Object>>> provinceCityMap = new HashMap<>();

        // 遍历原始数据，按省份整理城市数据
        for (Map<String, Object> city : cityCount) {
            String province = (String) city.get("province");
            String cityName = (String) city.get("city");
            long value = (Long) city.get("value");

            // 如果该省份已存在于映射表中，则将城市添加到对应省份的城市列表中，否则创建新的省份项
            if (provinceCityMap.containsKey(province)) {
                Map<String, Object> cityMap = new HashMap<>();
                cityMap.put("name", cityName);
                cityMap.put("value", value);

                provinceCityMap.get(province).add(cityMap);
            } else {
                List<Map<String, Object>> cityList = new ArrayList<>();
                Map<String, Object> cityMap = new HashMap<>();
                cityMap.put("name", cityName);
                cityMap.put("value", value);

                cityList.add(cityMap);
                provinceCityMap.put(province, cityList);
            }
        }

        // 将整理后的数据转换成新的格式，省份节点不包含值
        List<Map<String, Object>> transformedData = new ArrayList<>();
        for (Map.Entry<String, List<Map<String, Object>>> entry : provinceCityMap.entrySet()) {
            List<Map<String, Object>> cityData = entry.getValue();

            Map<String, Object> provinceMap = new HashMap<>();
            provinceMap.put("name", entry.getKey());
            provinceMap.put("children", cityData);

            transformedData.add(provinceMap);
        }

        return transformedData;
    }

    @Override
    public List<Map<String, Object>> getCity(SourceMemberDataInfoTemp sourceMemberDataInfoTemp) {
        List<Map<String, Object>> cityCount = sourceMemberDataInfoTempMapper.getCity(sourceMemberDataInfoTemp);
        return cityCount;
    }


    /**
     * 新增线索用户资质信息
     * 
     * @param sourceMemberDataInfoTemp 线索用户资质信息
     * @return 结果
     */
    @Override
    public int insertSourceMemberDataInfoTemp(SourceMemberDataInfoTemp sourceMemberDataInfoTemp)
    {
        sourceMemberDataInfoTemp.setCreateTime(DateUtils.getNowDate());
        return sourceMemberDataInfoTempMapper.insertSourceMemberDataInfoTemp(sourceMemberDataInfoTemp);
    }

    /**
     * 修改线索用户资质信息
     * 
     * @param sourceMemberDataInfoTemp 线索用户资质信息
     * @return 结果
     */
    @Override
    public int updateSourceMemberDataInfoTemp(SourceMemberDataInfoTemp sourceMemberDataInfoTemp)
    {
        sourceMemberDataInfoTemp.setUpdateTime(DateUtils.getNowDate());
        return sourceMemberDataInfoTempMapper.updateSourceMemberDataInfoTemp(sourceMemberDataInfoTemp);
    }

    /**
     * 批量删除线索用户资质信息
     * 
     * @param ids 需要删除的线索用户资质信息主键
     * @return 结果
     */
    @Override
    public int deleteSourceMemberDataInfoTempByIds(Long[] ids)
    {
        return sourceMemberDataInfoTempMapper.deleteSourceMemberDataInfoTempByIds(ids);
    }

    /**
     * 删除线索用户资质信息信息
     * 
     * @param id 线索用户资质信息主键
     * @return 结果
     */
    @Override
    public int deleteSourceMemberDataInfoTempById(Long id)
    {
        return sourceMemberDataInfoTempMapper.deleteSourceMemberDataInfoTempById(id);
    }
}
